import java.util.HashMap;
import java.util.Map;

/**
 * Parsing table
 * 
 * @author Chen Chen
 * 
 */
public class ParsingTable {
	Map<String, ProductionRule> table = new HashMap<String, ProductionRule>();

	public ParsingTable() {
	}

	/**
	 * sets a table entry
	 * 
	 * @param A
	 *            non-terminal
	 * @param a
	 *            tokem
	 * @param rule
	 *            production rule
	 * @return if succeed
	 */
	public boolean set(String A, String a, ProductionRule rule) {

		if (table.containsKey(A + " " + a))
			return false;
		else {
			table.put(A + " " + a, rule);
			return true;
		}

	}

	/**
	 * gets a table entry
	 * 
	 * @param A
	 *            non-terminal
	 * @param a
	 *            token
	 * @return {@link ProductionRule} in [A,a]
	 */
	public ProductionRule get(String A, String a) {
		return table.get(A + " " + a);
	}
	
	public String toString(){
		StringBuilder out=new StringBuilder();
		for(String key:table.keySet()){
			out.append('[');
			out.append(key);
			out.append("] : ");
			out.append(table.get(key).toString());
			out.append('\n');
		}
		return out.toString();
	}
}
